package define

import (
	"crypto/sha1"
	"fmt"
	"sort"
	"strings"
)

type MessageEntity struct {
	Signature string `form:"signature"`
	Timestamp string `form:"timestamp"`
	Nonce     string `form:"nonce"`
	EchoStr   string `form:"echostr"`
}

type WechatAccessToken struct {
	AccessToken string `json:"access_token"`
}

func (m *MessageEntity) CheckSignature(token string) bool {
	item := []string{token, m.Timestamp, m.Nonce}
	sort.Strings(item)
	itemByte := strings.Join(item, "")
	signature := fmt.Sprintf("%x", sha1.Sum([]byte(itemByte)))
	return signature == m.Signature
}
